﻿namespace Application.Utils
{
    public class CacheHelper
    {
        /// <summary>
        /// 设置分布式缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="RedisKey"></param>
        /// <param name="func"></param>
        /// <param name="expireSeconds">过期(秒单位)、默认不过期</param>
        /// <returns></returns>
        public static async Task<T> GetCacheOrDb<T>(string RedisKey, Func<Task<T>> func, int expireSeconds = -1, bool forceUpdate = false)
        {
            var cacheList = await RedisHelper.GetAsync<T>(RedisKey);
            if (cacheList != null && !forceUpdate) return cacheList;
            var result = await func();
            await RedisHelper.SetAsync(RedisKey, result, expireSeconds);
            return result;
        }
    }
}
